package com.zhentao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.pojo.Cuisine;
import com.zhentao.pojo.Orders;
import com.zhentao.pojo.Shopping;
import com.zhentao.service.CuisineService;
import com.zhentao.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cuisine")
public class CuisineController {
    @Autowired
    CuisineService cuisineService;
//    分页查询
    @PostMapping("findCuisine")
    public PageInfo<Cuisine> findCuisine(@RequestBody Query query){
        System.out.println(query+"11111111111113+++++++++++++++++");
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<Cuisine> cuisine = cuisineService.findCuisine(query);
        System.out.println(cuisine+"++++++++++++++++++++++++++++++++++++++++++++++++");
        PageInfo<Cuisine> pageInfo = new PageInfo<>(cuisine);
        return pageInfo;
    }
//    添加购物车
    @PostMapping("ShopAdd")
    public Boolean ShopAdd(@RequestBody Query query){
        System.out.println(query+"++++++++++++++++++++++++++++++++++++++++++++++++");
        try {
            Shopping shopping = new Shopping();
            shopping.setSname(query.getCname());
            shopping.setPrice(query.getPrice());
            shopping.setImg(query.getImg());
            shopping.setSum(query.getSum());
            cuisineService.ShopAdd(shopping);
            return true;
        }catch (Exception e){
            return false;
        }
    }
//    分页查询购物车
    @PostMapping("findShop")
    public PageInfo<Shopping> findShop(@RequestBody Query query){
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<Shopping> shop = cuisineService.findShop();
        PageInfo<Shopping> pageInfo = new PageInfo<>(shop);
        return pageInfo;
    }
//    删除购物车
    @DeleteMapping("DelShop")
    @Transactional
    public boolean DelShop(@RequestBody Query query){
        try {
            cuisineService.DelShop(query);
            Orders orders=new Orders();
            orders.setOname(query.getSname());
            orders.setSum(query.getSum());
            orders.setPrice(query.getPrice());
            orders.setImg(query.getImg());
            cuisineService.AddOrder(orders);
            return true;
        }catch (Exception e){
            return false;
        }
    }
//    分页查询订单
    @PostMapping("ordersAll")
    public PageInfo<Orders> ordersAll(@RequestBody Query query){
        System.out.println(query+"11111111111113+++++++++++++++++");
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<Orders> orders = cuisineService.ordersAll(query);
        System.out.println(orders+"++++++++++++++++++++++++++++++++++++++++++++++++");
        PageInfo<Orders> pageInfo = new PageInfo<>(orders);
        return pageInfo;
    }
//    修改菜单
    @PostMapping("updatecuisine")
    public Boolean updateCuisine(@RequestBody Query query){
        System.out.println(query+"11111111111113+++++++++++++++++");
        try {
            Cuisine cuisine = new Cuisine();
            cuisine.setCname(query.getCname());
            cuisine.setPrice(query.getPrice());
            cuisine.setImg(query.getImg());
            cuisineService.updateCuisine(cuisine);
            return true;
        }catch (Exception e){
            return false;
        }
    }
//    删除菜单
    @DeleteMapping("deletecuisine")
    public boolean deleteCuisine(@RequestBody Query query){
        System.out.println(query+"11111111111113+++++++++++++++++");
        try {
            cuisineService.deleteCuisine(query);
            return true;
        }catch (Exception  e){
            return false;
        }
    }
    @PostMapping("addcuisine")
    public Boolean addCuisine(@RequestBody Cuisine query){
        System.out.println(query+"11111111111113+++++++++++++++++");

            cuisineService.addCuisine(query);
            return true;

    }
//    修改订单
    @PostMapping("ordersUpdate")
    public boolean ordersUpdate(@RequestBody Orders orders){
        try {
            cuisineService.ordersUpdate(orders);
            return true;
        }catch (Exception e){
            return false;
        }
    }
//    删除购物车
    @DeleteMapping("Del")
    public boolean Del(@RequestBody int sid){
        try {
            cuisineService.Del(sid);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
